﻿////////////////////////////////////////////////////////////////////////////////
//
// Licensed under the MIT License
//
// Copyright (c) 2008 Chris Colinsky
// All Rights Reserved
//
// version	0.4
// http://code.google.com/p/sugarcookie/
// http://code.google.com/p/sugarcookie/wiki/License
//
////////////////////////////////////////////////////////////////////////////////
 
import sugarcookie.ui.UIMovieClip;
import sugarcookie.video.FLVLoader;
import sugarcookie.logger.*;

/**
 * Class extends UIMovieClip...works in conjunction with FLVLoader class to play back FLV files
 * PLEASE NOTE INSTANCE NAMES FOR THE BUTTONS, PLAYHEAD, and LOADING/PROGRESS BARS
 */ 
 class sugarcookie.video.FLVController extends UIMovieClip{
	 
	 private var _log:Logger;
	 
	 private var display:Video;
	 private var loader:FLVLoader;
	 
	 //buttons
	 private var btn_sound:MovieClip;
	 private var btn_beginning:MovieClip;
	 private var btn_play:MovieClip;
	 private var btn_pause:MovieClip;
	 private var btn_stop:MovieClip;
	 
	 //playhead
	 private var btn_playhead:MovieClip;
	 private var dragging:Boolean = false;
	 
	 //loading and progress
	 private var progress_bar:MovieClip;
	 private var loading_bar:MovieClip;
	 private var bar_progressBkgd:MovieClip;
	 
	 public function FLVController(){
		super(this);
		_log = Logger.getLog();
		_log.addObserver(new OutputPanelView(_log));
		_log.addObserver(new LoggerConsoleView(_log));
		setBehaviors();
	 }
	 
	 public function associateDisplay(v:Video):Void{
		display = v; 
		loader = new FLVLoader(display, this);
		loader.addEventListener("onStatus", updateStatus);
	 }
	 
	 public function loadVideo(url:String, buffer:Number):Void{
		loader.loadVideo(url, buffer); 
		//loader.loadVideo("fpo_demo_video.flv", 3); 
	 }
	 
	 /*
	  * Error handling happens here
	  */
	 
	 private function updateStatus(evt:Object):Void{
	 	_log.debug("updateStatus: \n"+evt.msg);
		if(evt.msg=="NetStream.Play.StreamNotFound"){
			throw new Error("NetStream.Play.StreamNotFound");
		}else if(evt.msg=="NetStream.Seek.InvalidTime"){
			throw new Error("NetStream.Seek.InvalidTime");
		}else{
			
		}
	 }
	 
	 private function setBehaviors():Void{
		 var rObj:Object = this;
		 btn_sound.onRelease = function():Void{
			 rObj.postEvent("toggleSound", "TOGGLE SOUND", true, true, {});
			 //rObj.log.info("btn_sound.onRelease with this._parent: "+rObj);
			 //trace("btn_sound.onRelease with this._parent: "+rObj);
		 }
		 btn_pause.onRelease = function():Void{
			 rObj.postEvent("pauseStream", "PAUSE", true, true, {});
		 }
		 btn_play.onRelease = function():Void{
			 rObj.postEvent("playStream", "PLAY", true, true, {});
		 }
		 btn_stop.onRelease = function():Void{
			 rObj.postEvent("stopStream", "STOP", true, true, {});
		 }
		 btn_beginning.onRelease = function():Void{
			 rObj.postEvent("seekStart", "SEEK TO START", true, true, {});
		 }
		 btn_playhead.onPress = function():Void{
			this.startDrag(true, rObj.progress_bar._x, rObj.btn_playhead._y, rObj.loading_bar._width-rObj.btn_playhead.icon._width+rObj.progress_bar._x, rObj.btn_playhead._y);
			rObj.dragging = true; 
		 }
		 btn_playhead.onRelease = btn_playhead.onReleaseOutside = function():Void{
			this.stopDrag();
			var distance:Number = btn_playhead._x-progress_bar._x;
			rObj.progress_bar._width = distance;
			rObj.dragging = false;
		 }
	 }
	 
	 private function onEnterFrame():Void{
		 postEvent("enterFrame", "ENTER FRAME", true, false, {_progressBar:progress_bar, _loadingBar:loading_bar, _playhead:btn_playhead, _dragging:dragging, _background:bar_progressBkgd, _totalWidth:bar_progressBkgd._width});
	 }
 }